//
//  RegiestrVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "RegiestrVC.h"
#import "PrivacyOfUserDetailsVC.h"

@interface RegiestrVC ()
@property (nonatomic,strong) UIScrollView   * contentView;
@property (nonatomic,strong) UILabel        * nameLabel;
@property (nonatomic,strong) UILabel        * infoLabel;
@property (nonatomic,strong) UITextField    * phoneTextField;
@property (nonatomic,strong) UITextField    * passTextField;
@property (nonatomic,strong) UIButton       * passBtn;
@property (nonatomic,strong) UITextField    * passAgainTextField;
@property (nonatomic,strong) UIButton       * passAgainBtn;
@property (nonatomic,strong) UITextField    * codeTextField;
@property (nonatomic,strong) JKCountDownButton * downBtn;
@property (nonatomic,strong) UIButton       * enterBtn;
@property (nonatomic,strong) YYLabel        * reigestLabel;
@property (nonatomic,strong) UIButton       * agreeBtn;
@property (nonatomic,strong) YYLabel        * agreeLabel;
@property (nonatomic,assign) BOOL           isAgree;

@end

@implementation RegiestrVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navgationView hideInfo];
    [self setSubViewLayout];
}

-(void)setSubViewLayout{
    
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.infoLabel];
    [self.contentView addSubview:self.phoneTextField];
    [self.contentView addSubview:self.passTextField];
    [self.contentView addSubview:self.passAgainTextField];
    [self.contentView addSubview:self.codeTextField];
    [self.contentView addSubview:self.enterBtn];
    [self.contentView addSubview:self.reigestLabel];
    [self.contentView addSubview:self.agreeBtn];
    [self.contentView addSubview:self.agreeLabel];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)agreeBtnAction:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    self.isAgree = sender.selected;
    
}

-(void)passBtnAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.passTextField.secureTextEntry = NO;
    }else{
        self.passTextField.secureTextEntry = YES;
    }
}

-(void)passAgainBtnAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.passAgainTextField.secureTextEntry = NO;
    }else{
        self.passAgainTextField.secureTextEntry = YES;
    }
}

-(void)enterBtnAction{
    
    
    if (!self.isAgree) {
        FhudShowStr(@"请阅读并同意用户协议与隐私政策");
        return;
    }
        
    if ([self.phoneTextField.text isEqual:@""]) {
        FhudShowStr(@"请输入手机号");
        return;
    }
    if (self.phoneTextField.text.length != 11) {
        FhudShowStr(@"请输入正确手机号");
        return;
    }
    if ([self.passTextField.text isEqual:@""]) {
        FhudShowStr(@"请输入密码");
        return;
    }
    
    if (self.passTextField.text.length < 8) {
        FhudShowStr(@"密码长度在8到16个字符");
        return;
    }
    
    if (![self.passTextField.text containsAlphabetAndNumber]) {
        FhudShowStr(@"密码必须包含数字和英文字符");
        return;
    }
    
    if ([self.passAgainTextField.text isEqual:@""]) {
        FhudShowStr(@"请确认密码");
        return;
    }
    
    if (![self.passTextField.text isEqual:self.passAgainTextField.text]) {
        FhudShowStr(@"两次密码输入不一致，请重新输入");
        return;
    }
    
    if ([self.codeTextField.text isEqual:@""]) {
        FhudShowStr(@"请输入验证码");
        return;
    }
    
    @WeakObj(self)
    NSDictionary * parms = @{@"phone":self.phoneTextField.text ?:@"",@"pwd":self.passTextField.text ?:@"",@"smsCode":self.codeTextField.text ?: @""};
    [SPNetworkingIns POST:PORT(@"auth-center/api/log/register") params:parms success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        NSDictionary * dict = responseObject[@"data"];
        NSString * token = K_TimeJson(dict[@"token"], @"tokenValue");
        [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"token"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
        id vc = [[NSClassFromString(@"SetUserInfoVC") alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
            
}

-(UIScrollView *)contentView{
    
    if (!_contentView) {
        _contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _contentView.contentSize = CGSizeMake(WIDTH, [FMCommonTool getDeviceIsPad] ? 1600 : HEIGHT);
    }
    return _contentView;
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30 * FMScaleX, NavHeight + 12 * FMScaleX, 150 * FMScaleX, 45 * FMScaleX)];
        _nameLabel.textColor = RDRGB(0x714933);
        _nameLabel.font = Bold(32);
        _nameLabel.text = @"登录/注册";
    }
    return _nameLabel;
}

-(UILabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(GETMinX(self.nameLabel), GETMaxY(self.nameLabel) + 5 * FMScaleX, 180 * FMScaleX, 20 * FMScaleX)];
        _infoLabel.font = Regular(14);
        _infoLabel.textColor = RDRGB(0x714933);
        _infoLabel.text = @"中医养生戒指  助您酣然入睡";
    }
    return _infoLabel;
}

-(UITextField *)phoneTextField{
    
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.infoLabel) + 100 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _phoneTextField.font = Regular(16);
        _phoneTextField.textColor = RDRGB(0x714933);
        _phoneTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _phoneTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_phoneTextField addSubview:line];
    }
    return _phoneTextField;
}

-(UITextField *)passTextField{
    
    if (!_passTextField) {
        _passTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.phoneTextField) + 30 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _passTextField.font = Regular(16);
        _passTextField.textColor = RDRGB(0x714933);
        _passTextField.secureTextEntry = YES;
        _passTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请设置登录密码" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _passTextField.rightViewMode = UITextFieldViewModeAlways;
        _passTextField.rightView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 102*FMScaleX, 36*FMScaleX)];
            [view addSubview:self.passBtn];
            view;
        });
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _passTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_passTextField addSubview:line];
        _passTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _passTextField;
}

-(UIButton *)passBtn{
    
    if (!_passBtn) {
        _passBtn = [[UIButton alloc]initWithFrame:CGRectMake(65, 5 * FMScaleX, 30 * FMScaleX, 26 * FMScaleX)];
        [_passBtn setImage:[UIImage imageNamed:@"Login_pass_icon"] forState:UIControlStateNormal];
        [_passBtn setImage:[UIImage imageNamed:@"Login_pass_icon_sel"] forState:UIControlStateSelected];
        [_passBtn addTarget:self action:@selector(passBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _passBtn;
}

-(UITextField *)passAgainTextField{
    
    if (!_passAgainTextField) {
        _passAgainTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.passTextField) + 30 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _passAgainTextField.font = Regular(16);
        _passAgainTextField.textColor = RDRGB(0x714933);
        _passAgainTextField.secureTextEntry = YES;
        _passAgainTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"确认登录密码" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _passAgainTextField.rightViewMode = UITextFieldViewModeAlways;
        _passAgainTextField.rightView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 102*FMScaleX, 36*FMScaleX)];
            [view addSubview:self.passAgainBtn];
            view;
        });
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _passAgainTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_passAgainTextField addSubview:line];
        _passAgainTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _passAgainTextField;
}

-(UIButton *)passAgainBtn{
    
    if (!_passAgainBtn) {
        _passAgainBtn = [[UIButton alloc]initWithFrame:CGRectMake(65, 5 * FMScaleX, 30 * FMScaleX, 26 * FMScaleX)];
        [_passAgainBtn setImage:[UIImage imageNamed:@"Login_pass_icon"] forState:UIControlStateNormal];
        [_passAgainBtn setImage:[UIImage imageNamed:@"Login_pass_icon_sel"] forState:UIControlStateSelected];
        [_passAgainBtn addTarget:self action:@selector(passAgainBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _passAgainBtn;
}

-(UITextField *)codeTextField{
    
    if (!_codeTextField) {
        _codeTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.passAgainTextField) + 30 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _codeTextField.font = Regular(16);
        _codeTextField.textColor = RDRGB(0x714933);
        _codeTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _codeTextField.rightViewMode = UITextFieldViewModeAlways;
        _codeTextField.rightView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 102*FMScaleX, 36*FMScaleX)];
            [view addSubview:self.downBtn];
            view;
        });
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _codeTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_codeTextField addSubview:line];
        _codeTextField.keyboardType = UIKeyboardTypeNumberPad;

    }
    return _codeTextField;
}

-(JKCountDownButton *)downBtn{
    if (!_downBtn) {
        _downBtn = [[JKCountDownButton alloc]initWithFrame:CGRectMake(0, 5 * FMScaleX, 89 * FMScaleX, 26 * FMScaleX)];
        [_downBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_downBtn setTitleColor:RDRGB(0x714933) forState:UIControlStateNormal];
        [_downBtn setCornerRadius:4 * FMScaleX borderWidth:1 borderColor:RDRGB(0xCDC1B8)];
        [_downBtn setBackgroundColor: UIColor.clearColor];
        _downBtn.titleLabel.font = Regular(13);
        @WeakObj(self)
        [_downBtn countDownButtonHandler:^(JKCountDownButton *countDownButton, NSInteger tag) {

            @StrongObj(self)
            if ([self.phoneTextField.text isEqual:@""]) {
                FhudShowStr(@"请输入手机号");
                return;
            }
            if (self.phoneTextField.text.length != 11) {
                FhudShowStr(@"请输入正确手机号");
                return;
            }
            
            NSDictionary * dict = @{@"phone":self.phoneTextField.text ? : @"",@"codeType":@"register"};
            [SPNetworkingIns GET:PORT(@"auth-center/api/log/smsPhone") params:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
                [countDownButton startCountDownWithSecond:59];
                countDownButton.userInteractionEnabled = NO;
                FhudShowStr(responseObject[@"msg"])
            } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                    
            }];
        }];
        [_downBtn countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.userInteractionEnabled = YES;
            return @"重新获取";
        }];
    }
    return _downBtn;
}

-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.codeTextField) + 50 * FMScaleX, WIDTH - 60 * FMScaleX, 50 * FMScaleX)];
        [_enterBtn setBackgroundImage:[UIImage imageNamed:@"Login_enter_icon"] forState:UIControlStateNormal];
        [_enterBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction ) forControlEvents:UIControlEventTouchUpInside];
        _enterBtn.titleLabel.font = Bold(20);
    }
    return _enterBtn;
}

-(YYLabel *)reigestLabel{
    if (!_reigestLabel) {
        _reigestLabel = [[YYLabel alloc]initWithFrame:CGRectMake((WIDTH - 180 * FMScaleX) / 2, GETMaxY(self.enterBtn) + 20 * FMScaleX, 180 * FMScaleX, 20 * FMScaleX)];
        _reigestLabel.textColor = RDRGB(0x714933);
        NSMutableAttributedString * attri = [[NSMutableAttributedString alloc]initWithString:@"已有账号？立即登录" attributes:@{NSForegroundColorAttributeName:RDRGB(0x714933)}];
        @WeakObj(self)
        [attri yy_setTextHighlightRange:[attri.string rangeOfString:@"立即登录"] color:RDRGB(0xB6904A) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
           @StrongObj(self)
            [self.navigationController popViewControllerAnimated:YES];
        }];
        _reigestLabel.attributedText = attri;
        _reigestLabel.textAlignment = NSTextAlignmentCenter;
        _reigestLabel.font = Regular(14);
    }
    return _reigestLabel;
}

-(UIButton *)agreeBtn{
    if (!_agreeBtn) {
        _agreeBtn = [[UIButton alloc]initWithFrame:CGRectMake(30 * FMScaleX, HEIGHT - kSafeAreaHeight - 20 * FMScaleX, 16 * FMScaleX, 16 * FMScaleX)];
        [_agreeBtn setImage:[UIImage imageNamed:@"Login_agree_no"] forState:UIControlStateNormal];
        [_agreeBtn setImage:[UIImage imageNamed:@"Login_agree_sel"] forState:UIControlStateSelected];
        [_agreeBtn addTarget:self action:@selector(agreeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeBtn;
}

-(YYLabel *)agreeLabel{
    if (!_agreeLabel) {
        _agreeLabel = [[YYLabel alloc]initWithFrame:CGRectMake(GETMaxX(self.agreeBtn) + 5 * FMScaleX, GETMinY(self.agreeBtn), WIDTH - 65 * FMScaleX, 16 * FMScaleX)];
        _agreeLabel.textColor = RDRGB(0x714933);
        NSMutableAttributedString * attri = [[NSMutableAttributedString alloc]initWithString:@"我已阅读并同意 用户协议 和 隐私政策" attributes:@{NSForegroundColorAttributeName:RDRGB(0x714933)}];
        @WeakObj(self)
        [attri yy_setTextHighlightRange:[attri.string rangeOfString:@"用户协议"] color:RDRGB(0xB6904A) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
           @StrongObj(self)
            PrivacyOfUserDetailsVC * vc = [[PrivacyOfUserDetailsVC alloc]init];
            vc.type = userType;
            [self.navigationController pushViewController:vc animated:YES];
        }];
        [attri yy_setTextHighlightRange:[attri.string rangeOfString:@"隐私政策"] color:RDRGB(0xB6904A) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            @StrongObj(self)
            PrivacyOfUserDetailsVC * vc = [[PrivacyOfUserDetailsVC alloc]init];
            vc.type = privacyType;
            [self.navigationController pushViewController:vc animated:YES];
        }];
        _agreeLabel.attributedText = attri;
        _agreeLabel.font = Regular(14);

    }
    return _agreeLabel;
}
@end
